package com.central;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.util.JdbcConstants;
import com.central.common.model.ColumnInfo;
import com.central.common.model.IndexInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author Tindy
 * @date 2022/5/5
 * @describe
 */
public class T2 {
    public static void main(String[] args1) {
        LinkedHashMap<String, ColumnInfo> columnMap = new LinkedHashMap<>();
        ArrayList<IndexInfo> indexs = new ArrayList<>();

        String createTableDdl="CREATE TABLE `task_defined_log` (\n" +
                "  `id` int(11) NOT NULL AUTO_INCREMENT,\n" +
                "  `version` int(5) NOT NULL,\n" +
                "  `version_desc` varchar(255) DEFAULT NULL,\n" +
                "  `task_defined_id` int(11) NOT NULL,\n" +
                "  `task_type` enum('SHELL','SQL') NOT NULL,\n" +
                "  `task_content` text NOT NULL,\n" +
                "  `task_params` text NOT NULL,\n" +
                "  `create_time` datetime DEFAULT NULL,\n" +
                "  `update_time` datetime DEFAULT NULL,\n" +
                "  PRIMARY KEY (`id`) USING BTREE\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;";
        SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(createTableDdl, JdbcConstants.MYSQL);
        List<SQLStatement> stmtList = parser.parseStatementList();
        for (SQLStatement stmt : stmtList) {
            MySqlCreateTableStatement createTable = ((MySqlCreateTableStatement) stmt);
            String comment = ((SQLCharExpr) createTable.getComment()) != null ? ((SQLCharExpr) createTable.getComment()).getText() : "";
            for (SQLTableElement sqlTableElement : createTable.getTableElementList()) {
                if (sqlTableElement instanceof SQLColumnDefinition) {
                    SQLColumnDefinition element = (SQLColumnDefinition) sqlTableElement;
                    ColumnInfo column = new ColumnInfo();
                    String columnName = element.getName().toString();
                    if (columnName.matches("`.*`")) {
                        columnName = columnName.substring(1, columnName.length() - 1);
                    }
                    column.setColumnName(columnName);
                    column.setDataType(element.getDataType().getName());
                    String args = "";
                    for (SQLExpr sqlExpr : element.getDataType().getArguments()) {
                        if(sqlExpr instanceof SQLIntegerExpr){
                            args += ((SQLIntegerExpr) sqlExpr).getNumber() + ",";
                        }else if(sqlExpr instanceof SQLCharExpr){
                            args += ((SQLCharExpr) sqlExpr).getText() + ",";
                        }
                    }
                    if (!StringUtils.isBlank(args)) {
                        args = args.substring(0, args.length() - 1);
                    }
                    column.setArgs(args);
                    String columnComment = element.getComment() != null ? element.getComment().toString() : "";
                    if (columnComment.matches("'.*'")) {
                        columnComment = columnComment.substring(1, columnComment.length() - 1);
                    }
                    column.setColumnComment(columnComment);
                    column.setDefVal(element.getDefaultExpr() == null || element.getDefaultExpr() instanceof SQLNullExpr ? "":element.getDefaultExpr().toString());
                    for (SQLColumnConstraint sqlColumnConstraint : element.getConstraints()) {
                        if (sqlColumnConstraint instanceof SQLNotNullConstraint) {
                            column.setIsNotNull(true);
                            break;
                        }
                    }
                    columnMap.put(columnName, column);
                } else if (sqlTableElement instanceof MySqlPrimaryKey) {
                    MySqlPrimaryKey element = (MySqlPrimaryKey) sqlTableElement;

                    for (SQLSelectOrderByItem sqlSelectOrderByItem : element.getColumns()) {
                        String primaryKeyColumn = sqlSelectOrderByItem.getExpr().toString();
                        if (primaryKeyColumn.matches("`.*`")) {
                            primaryKeyColumn = primaryKeyColumn.substring(1, primaryKeyColumn.length() - 1);
                        }
                        ColumnInfo column = columnMap.get(primaryKeyColumn);
                        column.setIsPrimkey(true);
                    }
                } else if (sqlTableElement instanceof MySqlKey) {
                    MySqlKey element = (MySqlKey) sqlTableElement;
                    IndexInfo index = new IndexInfo();
                    String indexName = element.getName().toString();
                    if (indexName.matches("`.*`")) {
                        indexName = indexName.substring(1, indexName.length() - 1);
                    }
                    index.setIndexName(indexName);
                    StringBuilder indexColumns = new StringBuilder();
                    for (SQLSelectOrderByItem sqlSelectOrderByItem : element.getColumns()) {
                        String indexColumn = sqlSelectOrderByItem.getExpr().toString();
                        if (indexColumn.matches("`.*`")) {
                            indexColumn = indexColumn.substring(1, indexColumn.length() - 1);
                        }
                        indexColumns.append(indexColumn).append(",");
                    }
                    index.setColumns(indexColumns.deleteCharAt(indexColumns.length() - 1).toString());
                    if (element instanceof MySqlUnique) {
                        index.setIsUnique(true);
                    }
                    indexs.add(index);
                }
            }
        }
    }
}
